#
# output catcher for rpy
#

class Catcher:
    """ A python singleton for catching output from rpy"""

    class __impl:
        """ Implementation of the singleton interface """
        def __init__(self):
            self.text=[]
        def reset(self):
            self.text=[]
        def add(self,text):
            self.text.append(text)
        def getTextArray(self):
            return self.text
        def getText(self, sep=''):
            return sep.join(self.text)

    # storage for the instance reference
    __instance = None

    def __init__(self):
        """ Create singleton instance """
        # Check whether we already have an instance
        if Catcher.__instance is None:
            # Create and remember instance
            Catcher.__instance = Catcher.__impl()

        # Store instance reference as the only member in the handle
        self.__dict__['_Catcher__instance'] = Catcher.__instance

    def __getattr__(self, attr):
        """ Delegate access to implementation """
        return getattr(self.__instance, attr)

    def __setattr__(self, attr, value):
        """ Delegate access to implementation """
        return setattr(self.__instance, attr, value)

def catcher(s):
    Catcher().add(s)
    
